package com.prs.crm.service.sale.impl;

import java.util.List;
import java.util.Set;

import com.prs.crm.context.Constrains;
import com.prs.crm.dao.sale.SalesShippingDao;
import com.prs.crm.domain.flow.WorkFlowProcess;
import com.prs.crm.domain.sale.Returns;
import com.prs.crm.domain.sale.SalesContract;
import com.prs.crm.domain.sale.SalesContractDetail;
import com.prs.crm.domain.sale.SalesShipping;
import com.prs.crm.domain.sale.SalesShippingDetail;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductStore;
import com.prs.crm.service.impl.FlowServiceImpl;
import com.prs.crm.service.sale.SalesContractService;
import com.prs.crm.service.sale.SalesShippingService;
import com.prs.crm.service.stock.ProductStoreService;

public class SalesShippingServiceImpl extends
		FlowServiceImpl<SalesShipping, SalesShippingDao> implements
		SalesShippingService {

	private SalesContractService salesContractService;
	private ProductStoreService productStoreService;

	@Override
	public void onProcessEnd(WorkFlowProcess process) {
		super.onProcessEnd(process);
		SalesShipping salesShipping = this.getProcessEntity(process);
		Set<SalesShippingDetail> details = salesShipping.getDetails();
		SalesContract contract = salesShipping.getSalesContract();
		Set<SalesContractDetail> contractDetails = contract.getDetails();
		if (process.getResult() == Constrains.WORKFLOWPROCESS_RESULT_PASS) {
			for (SalesShippingDetail detail : details) {
				List<ProductStore> productStores = this
						.getProductStoreService().getProductStores(
								detail.getProduct(), salesShipping.getRoom());
				String detailstring = "";
				double number = 0;
				for (ProductStore productStore : productStores) {
					if (productStore.getProductNum() != 0.0) {
						if (productStore.getProductNum() >= (detail.getNumber() - number)) {
							productStore.setProductNum(productStore
									.getProductNum()
									- (detail.getNumber() - number));

							detailstring = detailstring
									+ productStore.getId()
									+ "p"
									+ Double.valueOf(
											detail.getNumber() - number)
											.intValue() + ",";
							break;
						} else {
							number = number + productStore.getProductNum();
							detailstring = detailstring
									+ productStore.getId()
									+ "p"
									+ Double.valueOf(
											productStore.getProductNum())
											.intValue() + ",";
							productStore.setProductNum(0);
						}
					}
				}
				detail.setDetail(detailstring);
			}
			this.getDao().save(salesShipping);
		} else {
			for (SalesShippingDetail detail : details) {
				for (SalesContractDetail salesContractDetail : contractDetails) {
					if (detail.getProduct() == salesContractDetail.getProduct()) {
						salesContractDetail
								.setReceivesNumber(salesContractDetail
										.getReceivesNumber()
										- detail.getNumber());
						salesContractDetail.setRecivesState(0);
					}
				}
			}
			contract.setShippingState(0);
			this.getSalesContractService().save(contract);
		}
	}

	@Override
	public void saveSalesShipping(SalesShipping salesShipping,
			Product[] products, String[] pronames, Double[] originalCosts,
			Double[] nowCosts, Integer[] returnsNums, Double[] rebates,
			Double[] totalPrices, SalesContract[] conratcts) {
		for (int i = 0; i < products.length; i++) {
			SalesContractDetail saleOnject = this.getSalesContractService()
					.getSalesContractDetail(products[i], conratcts[i]);
			// saleOnject.setContractNum(saleOnject.getContractNum()-returnsNums[i]);
			// saleOnject.setTotalPrice(saleOnject.getTotalPrice()-totalPrices[i]);
			saleOnject.setReceivesNumber(saleOnject.getReceivesNumber()
					+ returnsNums[i]);
			if (saleOnject.getReceivesNumber() - saleOnject.getContractNum() == 0) {
				saleOnject.setRecivesState(1);
			}
			int sum = 0;
			List<SalesShipping> salesShippings = this.getDao()
					.getSalesShippingBySalesContract(conratcts[i]);
			if (salesShippings != null && salesShippings.size() != 0) {
				for (SalesShipping salesShipping2 : salesShippings) {
					List<SalesShippingDetail> salesShippingDetails = this
							.getDao().getAllDetailsBySalesShipping(
									salesShipping2);

					for (SalesShippingDetail salesShippingDetail : salesShippingDetails) {
						if (salesShippingDetail.getProduct() == products[i]) {
							sum = sum + salesShippingDetail.getNumber();
						}

					}
				}

			}

			sum = sum + returnsNums[i];

			SalesShippingDetail shippOnject = new SalesShippingDetail();
			shippOnject.setName(pronames[i]);
			shippOnject.setProduct(products[i]);
			shippOnject.setNumber(returnsNums[i]);
			shippOnject.setFixedPrice(nowCosts[i]);
			shippOnject.setRebate(rebates[i]);
			shippOnject.setTotalPrice(totalPrices[i]);
			shippOnject.setOprice(originalCosts[i]);
			shippOnject.setRoom(salesShipping.getRoom());
			shippOnject.setDetail("");
			salesShipping.addDetail(shippOnject);

			List<SalesContractDetail> contractDetails = this
					.getSalesContractService().contractDetails(conratcts[i]);
			boolean asdf = true;
			for (SalesContractDetail contractDetail : contractDetails) {
				if (contractDetail.getRecivesState() != 1) {
					asdf = false;
					break;
				}
			}
			if (asdf) {
				conratcts[i].setShippingState(1);
			}

		}
		salesShipping.setReceivesState(0);
		salesShipping.setReceiveNumber(0);
		super.save(salesShipping);

	}

	public boolean valideSubmit(SalesShipping entity) {

		return true;
	}

	public SalesContractService getSalesContractService() {
		return salesContractService;
	}

	public void setSalesContractService(
			SalesContractService salesContractService) {
		this.salesContractService = salesContractService;
	}

	@Override
	public List<SalesShippingDetail> getAllDetails() {

		return this.getDao().getAllDetails();
	}

	@Override
	public List<SalesShippingDetail> getAllDetailsBySalesShipping(
			SalesShipping salesShipping) {
		return this.getDao().getAllDetailsBySalesShipping(salesShipping);

	}

	public ProductStoreService getProductStoreService() {
		return productStoreService;
	}

	public void setProductStoreService(ProductStoreService productStoreService) {
		this.productStoreService = productStoreService;
	}

}
